<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var obj = {
            id: 1,
            name: 'andy',
            msg: {
                age: 18
            },
            color: ['pink', 'red']
        };


        //浅拷贝
        var o = {};
        // for (var k in obj) {
        //     //k是属性名，obj[k] 是属性值
        //     o[k] = obj[k];
        // }
        // console.log(o);
        // console.log('-------------------');
        // Object.assign(o, obj);
        // console.log(o);


        //深拷贝
        //封装函数
        function deepCopy(newobj, oldobj) {
            for (var k in oldobj) {
                //判断我们的属性值属于哪种数据类型
                //1.获取属性值 oldobj[k]
                var item = oldobj[k];
                //2.判断这个值是否是数组
                if (item instanceof Array) {
                    newobj[k] = [];
                    deepCopy(newobj[k], item)
                } else if (item instanceof Object) {
                    //3.看这个值是否属于对象
                    newobj[k] = {};
                    deepCopy(newobj[k], item)
                } else {
                    //4.属于简单类型
                    newobj[k] = item;
                }

            }
        }
        deepCopy(o, obj);
        console.log(o);
        o.msg.age = 20;
        console.log(obj);
    </script>
</body>

</html>